package com.erp.produce.service.impl;

import com.erp.common.util.ServiceUtil;
import com.erp.common.util.StringUtil;
import com.erp.produce.dao.CooperativesDao;
import com.erp.produce.entity.Cooperatives;
import com.erp.produce.service.CooperativeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CooperativeServiceImpl implements CooperativeService {

    @Autowired
    CooperativesDao dao;

    @Override
    public Boolean update(Cooperatives cooperatives) {
        Cooperatives cooperatives1 = dao.findById(cooperatives.getId()).get();
        BeanUtils.copyProperties(cooperatives,cooperatives1);
        return dao.saveAndFlush(cooperatives1) != null;
    }

    @Override
    public Boolean add(Cooperatives cooperatives) {
        return dao.save(cooperatives)!=null;
    }

    @Override
    public Boolean deleteIds(Long[] ids) {
        return ServiceUtil.deleteIds(ids,dao);
    }

    @Override
    public List<Cooperatives> listPage(int page, int size) {
        return dao.findAll(PageRequest.of(page,size)).getContent();
    }

    @Override
    public List<Cooperatives> listPageLike(int page, int size, String column) {
        return dao.findAllByNameLike(StringUtil.addLike(column),PageRequest.of(page, size,new Sort(Sort.Direction.ASC,"id")));
    }

    @Override
    public List<Cooperatives> list() {
        return dao.findAll(new Sort(Sort.Direction.ASC,"id"));
    }
}
